package io.nutz.nutzsite.module.acm.controller;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.nutz.dao.Cnd;
import org.nutz.dao.Sqls;
import org.nutz.ioc.loader.annotation.Inject;
import org.nutz.ioc.loader.annotation.IocBean;
import org.nutz.lang.Lang;
import org.nutz.lang.Strings;
import org.nutz.lang.util.NutMap;
import org.nutz.log.Log;
import org.nutz.log.Logs;
import org.nutz.mvc.annotation.At;
import org.nutz.mvc.annotation.Ok;
import org.nutz.mvc.annotation.POST;
import org.nutz.mvc.annotation.Param;
import org.nutz.plugins.slog.annotation.Slog;

import io.nutz.nutzsite.common.base.Result;
import io.nutz.nutzsite.module.acm.models.Contest;
import io.nutz.nutzsite.module.acm.models.ContestProblem;
import io.nutz.nutzsite.module.acm.models.Problem;
import io.nutz.nutzsite.module.acm.services.ContestProblemService;
import io.nutz.nutzsite.module.acm.services.ContestService;
import io.nutz.nutzsite.module.acm.services.ProblemService;
/**
 * acm竞赛采用的题目 信息操作处理
 * 
 * @author lrw
 * @date 2020-02-16
 */
@IocBean
@At("/acm/contestProblem")
public class ContestProblemController {
	private static final Log log = Logs.get();

	@Inject
	private ProblemService problemService;
	
	@Inject
	private ContestProblemService contestProblemService;
	@Inject
	private ContestService contestService;
	
	@RequiresPermissions("acm:contestProblem:view")
	@At("")
	@Ok("th:/acm/front/contestProblem.html")
	public void index(@Param("contestId") Integer contestId,HttpServletRequest req) {
		Contest contest=contestService.fetch(contestId);
        req.setAttribute("contest", contest);
	}

	/**
	 * 查询acm竞赛采用的题目列表
	 */
	//@RequiresPermissions("acm:contestProblem:list")
	@At
	@Ok("json")
	public Object list(@Param("pageNum")int pageNum,
					   @Param("pageSize")int pageSize,
					   @Param("contestId") Integer contestId,
					   //@Param("orderByColumn") String orderByColumn,
					   @Param("isAsc") String isAsc,
					   HttpServletRequest req) {
		Cnd cnd = Cnd.NEW();
		cnd.and("contestId", "=", contestId);
		String orderByColumn="num";
		return contestProblemService.tableList(pageNum,pageSize,cnd,orderByColumn,isAsc,null);
	}
	/**
	 * 新增acm竞赛采用的题目
	 */
	@At("/add")
	@Ok("th:/acm/contestProblem/add.html")
	public void add( HttpServletRequest req) {

	}

	/**
	 * 新增保存acm竞赛采用的题目
	 */
	@At
	@POST
	@Ok("json")
	@RequiresPermissions("acm:contestProblem:add")
	@Slog(tag="acm竞赛采用的题目", after="新增保存acm竞赛采用的题目 id=${args[0].id}")
	public Object addDo(@Param("..") ContestProblem contestProblem,HttpServletRequest req) {
		try {
			contestProblemService.insert(contestProblem);
			return Result.success("system.success");
		} catch (Exception e) {
			return Result.error("system.error");
		}
	}
	/**
	 * 新增保存acm竞赛采用的题目
	 */
	@At
	@POST
	@Ok("json")
	//@RequiresPermissions("acm:contestProblem:add")
	@Slog(tag="acm竞赛采用的题目", after="新增保存acm竞赛采用的题目 id=${args[0].id}")
	public Object addDo2(@Param("ids") String ids, @Param("contestId") Integer contestId,HttpServletRequest req) {
		try {//select name from ksqt where code in ("+qtc+") order by instr(',"+qtc+",',concat(',',code,','))
			//List<Problem> listp=problemService.query(Sqls.create(""));
			String[] indexList={"A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z"};
			String[] idss=ids.split(",");
			List<ContestProblem> listcp=contestProblemService.query(Cnd.where("contestId","=",contestId));
			if(listcp!=null && listcp.size()>0) contestProblemService.delete(Cnd.where("contestId","=",contestId));
			for(int i=0;i<idss.length;i++){
				int id=Integer.valueOf(idss[i]);
				ContestProblem cp=new ContestProblem();
				cp.setProblemId(id);
				String num=indexList[i];
				cp.setNum(num);
				cp.setTitle("Problem "+num);
				cp.setContestId(contestId);
				
				contestProblemService.insert(cp);
			}
			
			return Result.success("system.success");
		} catch (Exception e) {
			return Result.error("system.error");
		}
	}
	/**
	 * 修改acm竞赛采用的题目
	 */
	@At("/edit/?")
	@Ok("th://acm/contestProblem/edit.html")
	public void edit(String id, HttpServletRequest req) {
		ContestProblem contestProblem = contestProblemService.fetch(id);
		req.setAttribute("contestProblem",contestProblem);
	}

	/**
	 * 修改保存acm竞赛采用的题目
	 */
	@At
	@POST
	@Ok("json")
	@RequiresPermissions("acm:contestProblem:edit")
	@Slog(tag="acm竞赛采用的题目", after="修改保存acm竞赛采用的题目")
	public Object editDo(@Param("..") ContestProblem contestProblem,HttpServletRequest req) {
		try {
			if(Lang.isNotEmpty(contestProblem)){

				contestProblemService.update(contestProblem);
			}
			return Result.success("system.success");
		} catch (Exception e) {
			return Result.error("system.error");
		}
	}

	/**
	 * 删除acm竞赛采用的题目
	 */
	@At("/remove")
	@Ok("json")
	@RequiresPermissions("acm:contestProblem:remove")
	@Slog(tag ="acm竞赛采用的题目", after= "删除acm竞赛采用的题目:${array2str(args[0])}")
	public Object remove(@Param("ids")String[] ids, HttpServletRequest req) {
		try {
			contestProblemService.delete(ids);
			return Result.success("system.success");
		} catch (Exception e) {
			return Result.error("system.error");
		}
	}

}
